package com.tc.filter;

import com.tc.controller.SessionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Auther: tianchao
 * @Date: 2024/12/30 13:21
 * @Description:
 */
@Component
public class SessionFilter implements Filter {

    Logger log = LoggerFactory.getLogger(SessionFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("请求路径： {}",((HttpServletRequest)request).getServletPath());
        if (!(request instanceof HttpServletRequest)){
            chain.doFilter(request, response);
            return;
        }

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String path = httpServletRequest.getServletPath();
        if (!path.contains("/test1")){
            chain.doFilter(request, response);
            return;
        }
        HttpSession session = httpServletRequest.getSession(false);
        if (session == null){
            chain.doFilter(request, response);
            return;
        }
        long lastAccessedTime = session.getLastAccessedTime();
        // long lastAccessedTime = session.getLastAccessedTime();
        chain.doFilter(request, response);
        session.setAttribute("lastAccessedTime", 999L);
        System.out.println("========================");
    }
}
